var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
import checkLogin from '../../utils/checkLogin.js';

Page({ 
  data:{
    btnTxt: "获取验证码",
    btnUse:false,
    isBind: false //是否绑定手机号
  },

  onLoad:function(options){
    checkLogin.login(this, function (res) {
      console.log(res) 
    })
    console.log(options)
    var nickName = wx.getStorageSync('getUserInfo') && wx.getStorageSync('getUserInfo').nickName; //接收人名字
    var userName = options.userName; //赠送人名字
    var avatarUrl = options.avatarUrl;  //赠送人图片
    var times = options.times;  //日期
    var pkgCutId = options.pkgCutId;
    var coverMain = options.coverMain; //专辑图片
    var name = options.name;
    var isBind = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').isBind;
    this.setData({
      userName: userName,
      avatarUrl: avatarUrl,
      times: times,
      pkgCutId: pkgCutId,
      nickName: nickName,
      coverMain: coverMain,
      name:name,
      isBind
    })
  },

  //设置手机号
  setPhone:function(e){
    var userPhone = e.detail.value;
    console.log(userPhone)
    this.setData({
      "userPhone":userPhone
    })
  },


  //点击获取验证码
  getVercode:function(){
    var _this = this;
    var userPhone = this.data.userPhone;
    if (!userPhone){
      wx.showToast({
        title:"请输入手机号码",
        icon:"loading",
        duration:3000
      })
    } else if (!(/^1[34578]\d{9}$/.test(userPhone))){
      wx.showToast({
        title: "手机号码有误",
        icon: "loading",
        duration: 2000
      })
    }else{
      _this.startCount();
      var param = {
        "mobile": userPhone,
      }
      ajaxhelper.post(app.globalData.frontJSHost + "genVCode", param, _this, function (res) {
        console.log(res);
        if (res.result) {
          var ticket = res.data && res.data.ticket;
          console.log(ticket)
          this.setData({
            ticket: ticket
          })
        }
      }, function (res) {
        console.log(res)
      });
      
    }
   
  },

  //设置验证码
  setVercode:function(e){
    var vercode = e.detail.value;
    console.log(vercode)
    this.setData({
      "vercode": vercode
    })
  },

  startCount:function(){
    var _this = this;
    var num = 60;
    var timer = setInterval(
      function(){
        num--;
        var str = num + "s后可重新发送";
        _this.setData({
          btnTxt:str,
          btnUse:true
        })

        if(num == 0){
          clearInterval(timer);
          _this.setData({
            btnTxt: "获取验证码",
            btnUse: false
          })
        }
      },1000
    ) 
  },  
  //点击跳转领取成功
  gainGift: function () {
    var userPhone = this.data.userPhone;
    console.log(userPhone)
    var vercode = this.data.vercode;
    console.log(vercode)
    var pkgCutId = this.data.pkgCutId;
    console.log(pkgCutId)
    var ticket = this.data.ticket;
    console.log(ticket)
    if (!this.data.isBind){  
      if (!userPhone) {
        wx.showToast({
          title: "请输入手机号码",
          icon: "loading",
          duration: 2000
        })
      } else if (!(/^1[34578]\d{9}$/.test(userPhone))) {
        wx.showToast({
          title: "手机号码有误",
          icon: "loading",
          duration: 2000
        })
      } else if (!vercode) {
        wx.showToast({
          title: "请输入验证码",
          icon: "loading",
          duration: 2000
        })
      } 
    }
      var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
      ajaxhelper.put(app.globalData.frontJSHost + "present/receive/" + this.data.pkgCutId + "?token=" + encodeURIComponent(token) + "&telephone=" + userPhone + "&smsCode=" + vercode + "&ticket=" + ticket, {}, this, this.setListData)  
  },
  setListData:function(res){
    console.log(res)
    var userName = this.data.userName;
    var coverMain = this.data.coverMain;
    console.log(userName)
    console.log(coverMain)
    if (res.resultStatus == 200){
      
      //如果没有绑定过 下订单之后就要存储新的绑定值
      if (!this.data.isBind) {
        wx.getStorage({
          key: 'Token_date',
          success: function (resultS) {
            console.log(resultS);
            var Token_date = resultS.data;
            Token_date.isBind = res.data.isBind;
            wx.setStorage({
              key: 'Token_date',
              data: Token_date,
            })
          },
        })
      }  
      wx.showToast({
        title: "领取成功",
        icon: "success",
        duration:2000,
        success:function(){
          wx.redirectTo({
            url: '../giveSuccess/giveSuccess?&userName=' + userName + '&coverMain=' + coverMain
          })
        }
      })     
    } else {
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
  },
 
  //返回跳转我的礼品卡
  return:function(){
    wx.navigateTo({
      url: '../giftCards/giftCards?&currentTab='+3
    })
  }
})